//
//  TF_AttentionView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/2/21.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_AttentionView: UIView {

    @objc public var XButton = UIButton()
    @objc public var QRView = UIImageView()
    @objc public var title = UILabel()
    @objc public  var saved:Bool = false
    override init(frame: CGRect) {
        super.init(frame: frame)


        self.addSubview(self.XButton)
        self.XButton.setImage(UIImage.init(named: "HTLive.bundle/qcX"), for: .normal)
        self.XButton.addTarget(self, action: #selector(XButtonClick), for: .touchUpInside)

        //TODO:弹窗关注

        self.QRView.backgroundColor = UIColor.clear
        self.addSubview(self.QRView)
        self.QRView.isUserInteractionEnabled = true

        // 长按
        let longPress = UILongPressGestureRecognizer.init(target: self, action: #selector(longPress(gesture:)))
        
        self.QRView.addGestureRecognizer(longPress)
  
        self.addSubview(self.title)
        
        self.title.text = "长按二维码保存到相册,\n用微信扫描关注";
        self.title.font = UIFont.PingFang(size: 16)
        
        
        self.title.textColor = UIColor(red: 81/255.0, green: 98/255.0, blue: 117/255.0, alpha: 1.0)
        
        self.title.textAlignment = .center
        self.title.numberOfLines = 0
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.XButton.frame = CGRect(x: self.frame.size.width - 9 - 20, y: 9, width: 20, height: 20)
        
        
        self.QRView.frame = CGRect(x: (self.frame.size.width - 115)/2, y: (self.frame.size.height - 115)/2, width: 115, height: 115)

        
        self.title.frame = CGRect(x: 0, y: 170, width: self.frame.size.width, height: 22.5 * 2) 
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //关闭关注
    @objc public func XButtonClick()
    {
        self.isHidden = true
    }
    //#pragma mark - 保存至相册
    @objc public func longPress(gesture:UILongPressGestureRecognizer)
    {
        
        if (gesture.state == .began) {
           
            self.becomeFirstResponder()
            //商店
            if (self.tag == 999) {
                TalkfunSharedTools.openUrl(image: self.QRView.image!)
              
                
                
            }else{
             
                //长安关注
                if (self.QRView.image == nil ) {
                    self.immediate(msg: "二维码不存在")
                }else if (self.saved == false) {
                    UIImageWriteToSavedPhotosAlbum(self.QRView.image!, self, #selector(saveComplete), nil)

                    
                }else{
                 
                    
                    self.immediate(msg: "保存图片成功")
                }
            }
            
            
        
        }
    }

    @objc public func immediate(msg:NSString)
    {
        self.isHidden = true
    }
    @objc func saveComplete(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
        if error != nil {
            self.TF_toast( "保存图片失败,请打开相册权限")
   
        } else {
            self.saved = true
            self.TF_toast( "保存图片成功")
         
        }
        }
    

    

}
